package org.jawa.learning.webapp.taglib;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 显示菜单导航条
 * 
 * @author Yak
 * @date 2012-09-23
 * 
 */
public class BreadcrumbTag extends TagSupport {
	private static final Logger Log = LoggerFactory.getLogger(BreadcrumbTag.class);

	protected JspWriter out;

	private Long navID;

	private Long pageID;

	// 菜单仓库在内存中的名称
	private String repository;

	// css Class
	private String cssClass;

	// 当前页面的 Class
	private String activeClass;

	/**
	 * 执行菜单标签
	 */
	public int doStartTag() throws JspException {

		MenuRepository repository = null;
		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
		String ctx = request.getContextPath();

		out = pageContext.getOut();

		navID = (Long) request.getAttribute("navID");

		if (navID == null) {
			Log.info("主导航菜单没有找到");
		}

		pageID = (Long) request.getAttribute("pageID");

		if (pageID == null) {
			Log.info("当前导航页菜单没有找到");
		}

		if (repository == null) {
			// 取出菜单仓库
			repository = (MenuRepository) pageContext.findAttribute(this.repository);
		}

		if (repository == null) {
			Log.info("菜单仓库不存在");
			return EVAL_PAGE;
		}

		try {
			out.println("<ul class=\"" + getCssClass() + "\">");
			out.println("<li>您现在的位置:</li>");
			if (null != navID) {
				MenuComponent menu = repository.getMenu(navID);
				if (null != menu) {
					String url = menu.getMenuUrl();
					
					out.println("<li><a");
					out.println("<a ");
					if (null != url) {
						out.println(" href=\"" + url + "\" ");
					}
					out.println(">" + menu.getMenuName() + "</a></li>");
					out.println("<li><span class=\"divider\">/</span></li>");
				}
			}
			if (null != pageID) {
				MenuComponent menu = repository.getMenu(pageID);
				if (null != menu) {
					MenuComponent parentMenu = menu.getParent();
					if (null != parentMenu) {
						
						if(!parentMenu.getId().equals(navID)){
							String url = parentMenu.getMenuUrl();
							
							out.println("<li><a");
							out.println("<a ");
							if (null != url) {
								out.println(" href=\"" + url + "\" ");
							}
							out.println(">" + parentMenu.getMenuName() + "</a></li>");
							
							out.println("<li><span class=\"divider\">/</span></li>");
						}
					}
					out.println("<li class=\"" + getActiveClass() + "\">" + menu.getMenuName() + "</li>");
				}
			}
			out.println("</ul>");
		} catch (IOException e) {
			Log.error("显示菜单导航出错!", e);
		}

		return EVAL_PAGE;
	}

	public void setRepository(String repository) {
		this.repository = repository;
	}

	public String getCssClass() {
		return cssClass;
	}

	public void setCssClass(String cssClass) {
		this.cssClass = cssClass;
	}

	public String getActiveClass() {
		return activeClass;
	}

	public void setActiveClass(String activeClass) {
		this.activeClass = activeClass;
	}
}
